<template>
  <div class="potentialCustomers">
    <page-header :title="'市场活动'"></page-header>
    <div class="search_area">
      <el-row>
        <el-form :form="queryForm" label-width="1rem">
          <el-col :span="8">
            <el-form-item label="活动名称">
              <el-select v-model="queryForm.activityName" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in nameList" :label="item.label" :value="item.value" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所有者">
              <el-select v-model="queryForm.owner" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in ownerList" :label="item.userName" :value="item.uuid" :key="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="线索id">
              <el-input type="text" v-model="queryForm.leadId"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item label="开始时间">
              <el-date-picker
                v-model="queryForm.startDate"
                style="width: 100%"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="结束时间">
              <el-date-picker
                v-model="queryForm.endDate"
                style="width: 100%"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-form>
        <el-col :span="24">
          <div class="btn_area">
            <el-button class="confrim" @click="btnClick">查询</el-button>
            <el-button class="cancle" @click="reset">重置</el-button>
            <el-button class="confrim" @click="add">新增</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <table-list :fields="fields" :list="list" :pagination="pagination" :total="total" @sizeChange="handleSizeChange" @currentChange="handleCurrentChange" @handle="onHandle"></table-list>
    <el-dialog :title="code == '1' ? '新增市场活动': '编辑市场活动'" :visible.sync="visible" @close="close" width="60%">
      <el-row>
        <el-form :form="marketForm" label-width="1.2rem">
          <el-col :span="11">
            <el-form-item label="活动名称" required>
              <el-select v-model="marketForm.activityName" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in nameList" :label="item.label" :value="item.value" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="所有者" required>
              <el-select v-model="marketForm.owner" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in ownerList" :label="item.userName" :value="item.uuid" :key="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="开始时间" required>
              <el-date-picker
                v-model="marketForm.startDate"
                style="width: 100%"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="结束时间" required>
              <el-date-picker
                v-model="marketForm.endDate"
                style="width: 100%"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="成本" required>
              <el-input v-model="marketForm.cost" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="描述" required>
              <el-input v-model="marketForm.described" type="textarea" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="创建人">
               <el-input disabled v-model="marketForm.createByName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="创建时间">
              <el-input disabled v-model="marketForm.createTime"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="更新人">
              <el-input disabled v-model="marketForm.updateByName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="更新时间">
              <el-input disabled v-model="marketForm.updateTime"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible = false">取 消</el-button>
        <el-button class="confrim" @click="confrim">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tableList from '@/components/table.vue'
import pageHeader from '@/components/pageHeader.vue'
import { del } from 'vue'
export default {
  name: 'potentialCustomers',
  components: {
    tableList,
    pageHeader
  },
  data() {
    return {
      visible: false,
      code: '',
      marketForm: {
        activityName: "",
        adminId: "",
        cost: "",
        createBy: "",
        createByName: "",
        createTime: "",
        described: "",
        endDate: "",
        id: 0,
        uuid: "",
        owner: "",
        startDate: "",
        updateBy: "",
        updateByName: "",
        updateTime: "",
      },
      queryForm: {
        activityName: '',
        owner: '',
        startDate: '',
        endDate: '',
        leadId: ''
      },
      list: [],
      fields: [
        {
          prop: 'rowNum',
          label: '序号',
          align: 'center'
        },
        {
          prop: 'ownerName',
          label: '所有者',
          align: 'center'
        },
        {
          prop: 'activityName',
          label: '活动名称',
          align: 'center'
        },
        {
          prop: 'startDate',
          label: '开始时间',
          align: 'center'
        },
        {
          prop: 'endDate',
          label: '结束时间',
          align: 'center'
        },
        {
          prop: 'createByName',
          label: '创建人',
          align: 'center'
        },
        {
          prop: 'oper',
          label: '操作',
          align: 'center',
          btns: [
            {type: 'update',desc:'编辑'}
          ]
        }
      ],
      pagination: {
        currentPage: 1,
        pageSize: 10
      },
      total: 100,
      nameList: [],
      uuid: '',
      ownerList: [],
      userData: {},
    }
  },
  methods: {
    add() {
      this.visible = true;
      this.code = '1';
      this.marketForm.createBy = this.userData.uuid;
      this.marketForm.createByName = this.userData.userName;
      this.marketForm.adminId = this.userData.uuid;
      this.marketForm.createTime = this.$time()
      console.log(this.marketForm);
    },
    confrim() {
      let url = this.code == '1' ? this.$api.market.create : this.$api.market.update;
      delete this.marketForm.id;
      this.$post(url,this.reqData(this.marketForm)).then(res => {
        if (res.data.code == 200) {
          this.$message.success(res.data.message);
          this.visible = false;
          this.queryList();
        } else {
          this.$message.error(res.data.message);
        }
      })
      console.log(this.marketForm);
      console.log(url);
    },
    close() {
      this.marketForm = {
        activityName: "",
        adminId: "",
        cost: "",
        createBy: "",
        createByName: "",
        createTime: "",
        described: "",
        endDate: "",
        id: 0,
        uuid: "",
        owner: "",
        startDate: "",
        updateBy: "",
        updateByName: "",
        updateTime: "",
      };
    },
    reset() {
      this.queryForm.activityName = '';
      this.queryForm.owner = '';
      this.queryForm.startDate = '';
      this.queryForm.endDate = '';
      this.queryForm.leadId = ''
    },
    btnClick() {
      this.queryList()
    },
    onHandle(data) {
      let {row,type} = data;
      this.visible = true;
      this.code = '2';
      this.$post(this.$api.market.detail,this.reqData(row.uuid)).then(res => {
        if (res.data.code == 200) {
          this.marketForm = res.data.data;
          this.marketForm.updateBy = this.userData.uuid;
          this.marketForm.updateByName = this.userData.userName;
          this.marketForm.updateTime = this.$time();
          this.marketForm.adminId = this.userData.uuid;
          console.log(this.marketForm);
        } else {
          this.$message.error(res.data.message)
        }
      })

    },
    handleSizeChange(val) {
      console.log(val);
      this.pagination.pageSize = val;
      this.queryList();
    },
    handleCurrentChange(val) {
      console.log(val);
      this.pagination.currentPage = val;
      this.queryList();
    },
    queryList() {
      let url = this.$api.market.list;
      this.$post(url,this.reqData(this.queryForm,this.pagination.currentPage,this.pagination.pageSize)).then(res => {
        if (res.data.code == 200) {
          let data = res.data.data
          this.list = data.list;
          this.list.forEach((item,index) => {
            item.rowNum = (this.pagination.currentPage - 1) * this.pagination.pageSize + index + 1
          })
          this.total = data.total;
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    queryNameList() {
      let url = this.$api.market.codeList
      this.$post(url).then(res => {
        if (res.data.code == 200) {
          let data = res.data.data;
          this.nameList = []
          data.forEach(element => {
            this.nameList.push({
              label: element,
              value: element
            })
          });
          console.log(this.nameList);
        }
      })
    },
    queryUserList:function() {
      let url = this.$api.user.list;
      this.$post(url).then(res => {
        if (res.data.code == 200) {
          this.ownerList = res.data.data;
        }
      })
    },
  },
  mounted() {
    this.userData = JSON.parse(sessionStorage.getItem('userData'))
    this.uuid = this.userData.uuid;
    this.queryUserList();
    this.queryNameList();
    this.queryList();
  }
}
</script>
<style scoped>
.potentialCustomers {
  background: #fff;
  overflow: hidden;
  padding-bottom: 0.15rem;
}
</style>